package cn.iocoder.yudao.module.blog.convert.improve;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.blog.controller.admin.improvechapter.vo.BlogImproveChapterRespVO;
import cn.iocoder.yudao.module.blog.controller.admin.improvesection.vo.BlogImproveSectionRespVO;
import cn.iocoder.yudao.module.blog.convert.improvechapter.BlogImproveChapterConvert;
import cn.iocoder.yudao.module.blog.convert.improvesection.BlogImproveSectionConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvechapter.BlogImproveChapterDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.improvesection.BlogImproveSectionDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import cn.iocoder.yudao.module.blog.controller.admin.improve.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.improve.BlogImproveDO;

/**
 * 课程 Convert
 *
 * @author 曹建伟
 */
@Mapper
public interface BlogImproveConvert {

    BlogImproveConvert INSTANCE = Mappers.getMapper(BlogImproveConvert.class);

    BlogImproveDO convert(BlogImproveCreateReqVO bean);

    BlogImproveDO convert(BlogImproveUpdateReqVO bean);

    BlogImproveRespVO convert(BlogImproveDO bean);

    default BlogImproveRespVO convert(BlogImproveDO bean, List<BlogImproveChapterDO> chapterDOList, List<BlogImproveSectionDO> sectionDOList){
         BlogImproveRespVO respVO = convert(bean);
         List<BlogImproveChapterRespVO> chapterList = new ArrayList<>();

         Map<Long, List<BlogImproveSectionRespVO>> sectionMap = new HashMap<>();
         for(int i = 0; i<sectionDOList.size(); i++){
             BlogImproveSectionRespVO sectionRespVO = BlogImproveSectionConvert.INSTANCE.convert(sectionDOList.get(i));
             if(!sectionMap.containsKey(sectionRespVO.getChapterId())){
                 sectionMap.put(sectionRespVO.getChapterId(), new ArrayList<BlogImproveSectionRespVO>());
             }

             sectionMap.get(sectionRespVO.getChapterId()).add(sectionRespVO);
         }

         for(int i =0; i<chapterDOList.size();i++){
             BlogImproveChapterRespVO chapterRespVO = BlogImproveChapterConvert.INSTANCE.convert(chapterDOList.get(i));
             chapterRespVO.setSections(sectionMap.get(chapterRespVO.getId()));
             chapterList.add(chapterRespVO);
         }

         respVO.setChapters(chapterList);
         return respVO;
    }

    List<BlogImproveRespVO> convertList(List<BlogImproveDO> list);

    PageResult<BlogImproveRespVO> convertPage(PageResult<BlogImproveDO> page);

    default PageResult<BlogImproveRespVO> convertPage(PageResult<BlogImproveDO> page, List<BlogImproveChapterDO> chapterDOList, List<BlogImproveSectionDO> sectionDOList){
        if ( page == null ) {
            return null;
        }

        PageResult<BlogImproveRespVO> pageResult = new PageResult<BlogImproveRespVO>();

        Map<Long, Map<Long, List<BlogImproveSectionRespVO>>> sectionMap = new HashMap<>();
        for(int i = 0; i<sectionDOList.size(); i++){
            if(!sectionMap.containsKey(sectionDOList.get(i).getImproveId())){
                sectionMap.put(sectionDOList.get(i).getImproveId(), new HashMap<>());
            }

            if(!sectionMap.get(sectionDOList.get(i).getImproveId()).containsKey(sectionDOList.get(i).getChapterId())){
                sectionMap.get(sectionDOList.get(i).getImproveId()).put(sectionDOList.get(i).getChapterId(), new ArrayList<>());
            }

            sectionMap.get(sectionDOList.get(i).getImproveId()).get(sectionDOList.get(i).getChapterId()).add(BlogImproveSectionConvert.INSTANCE.convert(sectionDOList.get(i)));
        }

        Map<Long, List<BlogImproveChapterRespVO>> chapterMap = new HashMap<>();
        for(int i = 0; i<chapterDOList.size(); i++){
            if(!chapterMap.containsKey(chapterDOList.get(i).getImproveId())){
                chapterMap.put(chapterDOList.get(i).getImproveId(), new ArrayList<>());
            }

            BlogImproveChapterRespVO chapterRespVO = BlogImproveChapterConvert.INSTANCE.convert(chapterDOList.get(i));
            if(sectionMap.get(chapterRespVO.getImproveId()) != null){
                chapterRespVO.setSections(sectionMap.get(chapterRespVO.getImproveId()).get(chapterRespVO.getId()));
            }else{
                chapterRespVO.setSections(new ArrayList<>());
            }

            chapterMap.get(chapterDOList.get(i).getImproveId()).add(chapterRespVO);
        }

        pageResult.setList( convertList( page.getList() ) );

        for(int i = 0; i<pageResult.getList().size(); i++){
            pageResult.getList().get(i).setChapters(chapterMap.get(pageResult.getList().get(i).getId()));
        }
        pageResult.setTotal( page.getTotal() );

        return pageResult;
    }

    List<BlogImproveExcelVO> convertList02(List<BlogImproveDO> list);

}
